package edu.wit.common.result;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.wit.common.exception.HgException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ResponseUtil {
    public static void out(HttpServletResponse response, R r) {
        ObjectMapper mapper = new ObjectMapper();
        response.setStatus(HttpStatus.OK.value());
        /**
         * spring 的类这个常量标记为过时，官方说是主流浏览器如谷歌符合正常规范，不需要设置字符编码了，那么那些非主流的浏览器呢？这样标记为过时是否有不妥呢？
         */
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        try {
            mapper.writeValue(response.getWriter(), r);
            response.getWriter().flush();
        } catch (IOException e) {
//            throw new HgException(201,"账号或密码错误");
            e.printStackTrace();
        }
    }
}
